package bom.controller.organization;


import bom.pojo.organization.UserRole;
import bom.service.i.organization.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @Author: Fancyears·Maylos·Malvis
 * @Description:
 * @Date: Created in  2018/11/27 16:32
 * @Modified By:
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private UserRoleService userRoleService;

    /**
     * 功能描述：根据用户的权限去加载角色数据
     *
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @RequestMapping(value = "/loadRoleTree", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> loadRoleTree(UserRole entity) {
        return userRoleService.loadRoleTree(entity);
    }


    /**
     * 功能描述：根据用户的权限去加载角色是否写权限树
     *
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    @RequestMapping(value = "/loadRoleWriteTree", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> loadRoleWriteTree(UserRole entity) {
        return userRoleService.loadRoleWriteTree(entity);
    }

    /**
     * 功能描述：实现批量删除数据字典的记录
     *
     * @param json
     * @return
     */
    @RequestMapping(value = "/removeBath", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> removeBath(String json) throws Exception {
        return userRoleService.removeByBatch(json);
    }


    /**
     * 功能描述：跳转到开通用户写权限页面
     *
     * @param entity
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/openEdit", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public String openEdit(UserRole entity, Model model) throws Exception {
        entity = userRoleService.get(entity);
        model.addAttribute("entity", entity);
        return "roleEdit";
    }


    /**
     * 功能描述：更新数据字典数据
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> update(@RequestBody UserRole entity) throws Exception {
        return userRoleService.updateRole(entity);
    }

    /**
     * 功能描述:保存用户权限信息
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/savePrivilege", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> saveWritePrivilege(@RequestBody UserRole entity) {
        return userRoleService.saveWritePrivilege(entity);
    }
}
